﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;

/// <summary>
/// Summary description for PhoneLoader
/// </summary>
public class PhoneLoader
{
	private PhoneLoader()
	{
	}

    static public void SerializeToXML(UserPhone phone)
    {
        if (!string.IsNullOrEmpty(phone.PhoneNum))
        {
            string filepath = HttpContext.Current.Server.MapPath(@"Temp/" + phone.PhoneNum + ".xml");

            XmlSerializer serializer = new XmlSerializer(typeof(UserPhone));
            TextWriter textWriter = new StreamWriter(filepath);
            serializer.Serialize(textWriter, phone);
            textWriter.Close();
        }
    }

    static public UserPhone DeserializeFromXML(string phoneNum)
    {
        if (!string.IsNullOrEmpty(phoneNum))
        {
            string filepath = HttpContext.Current.Server.MapPath(@"Temp/" + phoneNum + ".xml");

            if (!File.Exists(filepath))
            {
                //SerializeToXML(new UserPhone(phoneNum));
                SerializeToXML(new UserPhone(phoneNum));
            }

            XmlSerializer deserializer = new XmlSerializer(typeof(UserPhone));
            TextReader textReader = new StreamReader(filepath);
            UserPhone phone;
            phone = (UserPhone)deserializer.Deserialize(textReader);
            textReader.Close();

            return phone;
        }
        else
        {
            return new UserPhone();
        }
    }


    static public ProviderRange DeserializeProviderRangeFromXML()
    {
        string filepath = HttpContext.Current.Server.MapPath(@"App_Data/PhoneRange.xml");

        XmlSerializer deserializer = new XmlSerializer(typeof(ProviderRange));
        TextReader textReader = new StreamReader(filepath);
        ProviderRange range;
        range = (ProviderRange)deserializer.Deserialize(textReader);
        textReader.Close();

        return range;
        
    }
}